﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.SessionState;
using System.Web;
using lib;
using XQ.Framework.http;
using XQ.Framework.core;
using XQ.Framework.model;

namespace XQ.Framework.helper
{
    public class AccountHelper : IRequiresSessionState
    {
        //COOKIE
        public const string COOKIE_NAME = "HIWJCN_COOKIE";
        public const string COOKIE_LOGIN_ID = "USER_ID";
        public const string COOKIE_LOGIN_PASS = "USER_PASS";
        //SESSION
        public const string USER_NAME_KEY = "LOGIN_USER_NAME_KEY";
        public const string USER_ID_KEY = "LOGIN_USER_ID_KEY";
        public const string USER_FLAG_KEY = "LOGIN_USER_FLAG_KEY";

        //获取session中的登陆状态
        public static string GetSessionUserId(HttpSessionState session)
        {
            return HttpHelper.Session.GetSessionString(session, AccountHelper.USER_ID_KEY);
        }
        public static string GetSessionUserName(HttpSessionState session)
        {
            return HttpHelper.Session.GetSessionString(session, AccountHelper.USER_NAME_KEY);
        }
        public static string GetSessionUserFlag(HttpSessionState session)
        {
            return HttpHelper.Session.GetSessionString(session, AccountHelper.USER_FLAG_KEY);
        }
        //获取用户cookie的登陆账号和密码
        public static string GetCookieUserID(HttpContext context)
        {
            return HttpHelper.Cookie.GetCookie(context, AccountHelper.COOKIE_NAME, AccountHelper.COOKIE_LOGIN_ID);
        }
        public static string GetCookieUserPass(HttpContext context)
        {
            return HttpHelper.Cookie.GetCookie(context, AccountHelper.COOKIE_NAME, AccountHelper.COOKIE_LOGIN_PASS);
        }
        //登陆信息保存到session和cookie
        public static bool SetUserLogin(HttpContext context, XQ.Framework.model.UserModelBase model)
        {
            SetUserLogin(context, model, false);
            return true;
        }
        public static bool SetUserLogin(HttpContext context, XQ.Framework.model.UserModelBase model, bool saveCookie)
        {
            try
            {
                SetUserLogout(context);
                HttpHelper.Session.SetSession(context.Session, AccountHelper.USER_ID_KEY, model.UserID);
                HttpHelper.Session.SetSession(context.Session, AccountHelper.USER_NAME_KEY, model.NickName);
                HttpHelper.Session.SetSession(context.Session, AccountHelper.USER_FLAG_KEY, model.Flag);
                if (saveCookie)
                {
                    //cookie name is "hiwjcn_cookie" 设置10天过期
                    HttpHelper.Cookie.SetCookie(context, AccountHelper.COOKIE_NAME,
                        AccountHelper.COOKIE_LOGIN_ID, model.Email,
                        expires: 60 * 24 * 10);
                    HttpHelper.Cookie.SetCookie(context, AccountHelper.COOKIE_NAME,
                        AccountHelper.COOKIE_LOGIN_PASS, model.PassWord,
                        expires: 60 * 24 * 10);
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        //退出登录
        public static bool SetUserLogout(HttpContext context)
        {
            try
            {
                HttpHelper.Session.RemoveSession(context.Session,
                    AccountHelper.USER_ID_KEY,
                    AccountHelper.USER_NAME_KEY,
                    AccountHelper.USER_FLAG_KEY);
                //remove cookie name
                HttpHelper.Cookie.RemoveCookie(context, AccountHelper.COOKIE_NAME);
                return true;
            }
            catch (Exception e)
            { return false; }
        }
        //判断登陆状态
        public static bool IsUserLogin(HttpSessionState session)
        {
            string uid = string.Empty;
            return IsUserLogin(session, ref uid);
        }
        public static bool IsUserLogin(HttpSessionState session, ref string user_id)
        {
            UserModelBase model = GetLoginUser(session);
            if (model != null)
            {
                user_id = model.UserID;
                return true;
            }
            return false;
        }
        public static XQ.Framework.model.UserModelBase GetLoginUser(HttpSessionState session)
        {
            XQ.Framework.model.UserModelBase model = new model.UserModelBase();
            model.UserID = AccountHelper.GetSessionUserId(session);
            model.NickName = AccountHelper.GetSessionUserName(session);
            model.Flag = AccountHelper.GetSessionUserFlag(session);
            return ValidateHelper.IsAllPlumpString(model.UserID, model.NickName, model.Flag) ? model : null;
        }
        public static bool IsAdminLogin(HttpSessionState session)
        {
            return AccountHelper.IsUserLogin(session) &&
                AccountHelper.GetSessionUserFlag(session).ToLower() == "admin";
        }
        public static bool IsManagerLogin(HttpSessionState session)
        {
            if (AccountHelper.IsAdminLogin(session))
            {
                return true;
            }
            return AccountHelper.IsUserLogin(session) &&
                AccountHelper.GetSessionUserFlag(session).ToLower() == "manager";
        }

    }
}
